<?php

namespace app\api\model;

use think\Model;

class RolesPolicyRelation extends Model
{
    //列表
    public function RolesPolicyList($page,$pagesize){
        try {
            $list = $this->alias('d')
                ->field('*')
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);
            if($list){
                $info = ['status'=>true,'code'=>0,'msg'=>"获取列表成功！",'data'=>$list];
            }else{
                $info = ['status'=>true,'code'=>1,'msg'=>"获取列表失败！",'data'=>$list];
            }
            return $info;
        } catch (\Exception $e) {
            $error = ['status'=>false,'error'=>['code'=>$e->getCode(),'msg'=>$e->getMessage()],'data'=>''];
            return $error;
        }
    }
    //新增
    public function RolesPolicyAdd($roles_id,$policy_id,$status){
        try {
            $id = uuid();
            $data = [];
            $data['id'] = $id;
            $data['roles_id'] = $roles_id;
            $data['policy_id'] = $policy_id;
            $data['create_time'] = time();
            $data['status'] = $status;
            $resid = $this->insert($data);
            if($resid){
                $info = ['status'=>true,'code'=>0,'msg'=>"新增成功！",'data'=>$id];
            }else{
                $info = ['status'=>true,'code'=>1,'msg'=>"新增失败！",'data'=>$id];
            }
            return $info;
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return $error;
        }
    }
    //保存
    public function RolesPolicyEdit($id,$roles_id,$policy_id){
        try {
            $data = [];
            $data['roles_id'] = $roles_id;
            $data['policy_id'] = $policy_id;
            $data['update_time'] = time();
            $resid = $this->where('id','=',$id)->save($data);
            if($resid){
                $info = ['status'=>true,'code'=>0,'msg'=>"保存成功！",'data'=>$resid];
            }else{
                $info = ['status'=>true,'code'=>1,'msg'=>"保存失败！",'data'=>$resid];
            }
            return $info;
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return $error;
        }
    }
    //禁/启用
    public function RolesPolicySetStatus($id,$status){
        try {
            $data = [];
            $data['status'] = $status;
            $res = $this->where('id','=',$id)->save($data);
            if($res!==false){
                $info = ['status'=>true,'code'=>0,'msg'=>"操作状态成功！",'data'=>''];
            }else{
                $info = ['status'=>true,'code'=>1,'msg'=>"操作状态失败！",'data'=>''];
            }
            return $info;
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return $error;
        }
    }
    //彻底删除
    public function RolesPolicyDel($id){
        try {
            $res = $this->where('id','=',$id)->delete();
            if($res){
                $info = ['status'=>true,'code'=>0,'msg'=>"删除成功！",'data'=>''];
            }else{
                $info = ['status'=>true,'code'=>1,'msg'=>"删除失败！",'data'=>''];
            }
            return $info;
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return $error;
        }
    }
}
